clear;
close all;
clc;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates table 3 in the paper 
%------------------------------------------------------------------------%


factor = 0.01;             % CHOOSE THE SCALING FACTOR

% load ----------------------------------------------------------------------- 

file_name = '../01_data/es_quotes_5minute_EST';

% Call returns routine -----------------------------------------------------------------------

start_date              = datenum(1998,1,5);
finish_date             = datenum(2020,12,29);

winsorizing             = 0;
alpha                   = 0.99;
replace_option          = 1;

results                 = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
log_return_DbD_AD       = results.log_return_DbD_AD;
log_return_HbH_AD       = results.log_return_HbH_AD;
log_return_AD           = results.log_return_AD;
T                       = results.T;
caldt_daily             = results.caldt;

fprintf(1,'********************************\n')
fprintf(1,'Sample: Quotes          \n')
fprintf('begin %12s  \n',datestr(caldt_daily(1)))
fprintf('finish %12s  \n',datestr(caldt_daily(end)))
fprintf(1,'********************************\n')

% Call SV routine -----------------------------------------------------------------------

file_name                                = '../01_Data/es_trades_5minute_EST.mat';
results                                  = make_SV(file_name,start_date,finish_date);

caldt_trades = results.caldt;
EOD_signed_volume15                      = results.EOD_signed_volume15;
EOD_rel_signed_volume15                  = results.EOD_rel_signed_volume15;          
EOD_signed_volume                        = results.EOD_signed_volume;
EOD_rel_signed_volume                    = results.EOD_rel_signed_volume;
Signed_volume_15b15_AD                   = results.Signed_volume_15b15_AD;
rel_Signed_volume_15b15_AD               = results.rel_Signed_volume_15b15_AD;
ToD                                      = results.Time_of_day;

fprintf(1,'********************************\n')
fprintf(1,'Sample: trades          \n')
fprintf('begin %12s  \n',datestr(caldt_trades(1)))
fprintf('finish %12s  \n',datestr(caldt_trades(end)))
fprintf(1,'********************************\n')

% ----------------------------------------------------------------% 
% intersect
% ----------------------------------------------------------------% 

[C,IA,IB]                             = intersect(caldt_trades,caldt_daily);
caldt_daily                           = caldt_daily(IB);
rets_CTC                              = log_return_DbD_AD(IB,:);
log_return_HbH_AD                     = log_return_HbH_AD(IB,:);
log_return_AD                         = log_return_AD(IB,:);

caldt_trades                          = caldt_trades(IA);
EOD_signed_volume15                   = EOD_signed_volume15(IA,:);
EOD_rel_signed_volume15               = EOD_rel_signed_volume15(IA,:);
EOD_signed_volume                     = EOD_signed_volume(IA,:);
EOD_rel_signed_volume                 = EOD_rel_signed_volume(IA,:);

rets_HbH                              = log_return_HbH_AD; % re-name

% ----------------------------------------------------------------% 
% HOURLY SIGNED VOLUME
% ----------------------------------------------------------------% 

EOD_rel_signed_volume = [EOD_rel_signed_volume , sum(EOD_rel_signed_volume,2)]/factor;
    
 
% summary stats cells

counter = 0;
for n = 1 : 15
    counter                 = counter + 1;
    temp                    = EOD_rel_signed_volume(:,n);
    temp                    = getrid(temp,1,2);
    rets_cell_ON{1,counter} = temp;
end

counter = 0;
for n = 16 : 25
    counter                 = counter + 1;
    temp                    = EOD_rel_signed_volume(:,n);
    temp                    = getrid(temp,1,2);
    rets_cell_ID{1,counter} = temp;
end

fprintf(1,'*******************************************\n')
fprintf(1,' RSV :  \n')
fprintf(1,'*******************************************\n')

latex_out           = 0;
log_or_simple       = 1;
file_name           = '../00_Results/tables/summary_stats_RSV_a.tex';
rets_string         = {'18-19', '19-20', '20-21' , '21-22' , '22-23' , '23-24' , '24-01' , '01-02' , '02-03' , '03-04', '04-05' , '05-06' , '06-07' , '07-08' , '08-09'};
result              = summary_stats_SV(rets_cell_ON , rets_string , latex_out , log_or_simple , file_name);
file_name           = '../00_Results/tables/summary_stats_RSV_b.tex';
rets_string         = {'09-10' , '10-11' , '11-12' , '12-13' , '13-14', '14-15' , '15-16' , '16-17' , '17-18' , 'CTC'};
result              = summary_stats_SV(rets_cell_ID , rets_string , latex_out , log_or_simple , file_name);




